﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Platform.LanguageProcessing.IO
{

	/// <summary>
	/// 
	/// </summary>
	public class WordReader : TextReader
	{
		/// <summary>
		/// 
		/// </summary>
		/// <param name="filePath"></param>
		public WordReader( string filePath )
			: this( new StreamReader( File.OpenRead( filePath ) ) )
		{ }

		/// <summary>
		/// 
		/// </summary>
		/// <param name="input"></param>
		public WordReader( TextReader input )
			: base()
		{
			if ( input == null )
			{
				throw new ArgumentNullException( "input" );
			}
			this.BaseReader = input;
		}

		/// <summary>
		/// 
		/// </summary>
		protected TextReader BaseReader
		{ get; private set; }

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public bool ReadWord( out WordLine word )
		{
			string line = null;

			while ( true )
			{
				line = this.BaseReader.ReadLine();

				if ( line == null )
				{
					// Acabou
					word = new WordLine();
					return false;
				}
				else
				{
					int index =0;

					SubjectGender gender = SubjectGender.Neutral;
					Cardinality cardinality = Cardinality.None;

					while ( index < line.Length )
					{
						switch ( line[ index ] )
						{
							case '#':
								// ignora essa linha que é um comentário
								break;

							case '+':
								cardinality = Cardinality.MoreThanOne;
								break;

							case '?':
								cardinality = Cardinality.AnyButOne;
								break;

							case '*':
								cardinality = Cardinality.All;
								break;

							case '.':
								cardinality = Cardinality.Any;
								break;

							case '1':
								cardinality = Cardinality.JustOne;
								break;

							case 'm':
							case 'M':
								gender = SubjectGender.Male;
								break;

							case 'f':
							case 'F':
								gender = SubjectGender.Female;
								break;

							case ';':
								word = new WordLine( line.Substring( index + 1 ), gender, cardinality );
								return true;
						}
						index++;
					}
				}
			}
		}


	}

	public struct WordLine
	{
		public readonly SubjectGender Gender;
		public readonly Cardinality Cardinality;
		public readonly string Word;

		public WordLine( string word, SubjectGender gender, Cardinality cardinality )
		{
			this.Word = word;
			this.Gender = gender;
			this.Cardinality = cardinality;
		}


	}
}
